BetterPowerBarAltDB = BetterPowerBarAltDB or {}

PlayerPowerBarAlt:SetSize(64, 64)
PlayerPowerBarAlt:SetMovable(true)
--something usurps UIParent and makes you HUGE. aaaaand its Chinchilla.
--PlayerPowerBarAlt:SetParent(UIParent)
--PlayerPowerBarAlt.SetParent = function() end

--Don't manage the frame for me, thanks, and don't adjust other frames to account for it
UIPARENT_MANAGED_FRAME_POSITIONS["PlayerPowerBarAlt"] = nil
for k, v in next, UIPARENT_MANAGED_FRAME_POSITIONS do
	v.playerPowerBarAlt = nil
end

local cvar, cvarLabel = "alternateResourceText", "ALTERNATE_RESOURCE_TEXT"

local overlay = CreateFrame("Frame", nil, PlayerPowerBarAlt)
overlay:SetAllPoints()

local texture = overlay:CreateTexture()
texture:SetAllPoints()
texture:SetTexture(1, 1, 1, 0.1)
texture:Hide()
overlay.texture = texture

local locked = true
local moving = nil

overlay:EnableMouse(true)
overlay:SetScript("OnMouseDown", function(self, button)
	if button == "LeftButton" and not locked then
		PlayerPowerBarAlt:ClearAllPoints()
		PlayerPowerBarAlt:StartMoving()
		moving = true
	end
end)

overlay:SetScript("OnMouseUp", function(self, button)
	if button == "RightButton" then
		locked = not locked
		if locked then
			self.texture:Hide()
			self:EnableMouseWheel(false)
		else
			self.texture:Show()
			self:EnableMouseWheel(true)
		end

	elseif button == "MiddleButton" then
		PlayerPowerBarAlt:SetScale(1)
		BetterPowerBarAltDB.scale = nil

	elseif moving then
		moving = nil
		PlayerPowerBarAlt:StopMovingOrSizing()
		PlayerPowerBarAlt:SetUserPlaced(false)

		local point, _, _, x, y = PlayerPowerBarAlt:GetPoint(1)
		BetterPowerBarAltDB.point = point
		BetterPowerBarAltDB.x = x
		BetterPowerBarAltDB.y = y

	elseif IsShiftKeyDown() then
		local state = not GetCVarBool(cvar) and 1
		SetCVar(cvar, state, cvarLabel)
	end
end)

overlay:SetScript("OnMouseWheel", function(self, delta)
	local scale = PlayerPowerBarAlt:GetScale() + (0.1 * delta)
	PlayerPowerBarAlt:SetScale(scale)
	BetterPowerBarAltDB.scale = scale
end)

overlay:SetScript("OnShow", function(self)
	if BetterPowerBarAltDB.point then
		PlayerPowerBarAlt:ClearAllPoints()
		PlayerPowerBarAlt:SetPoint(BetterPowerBarAltDB.point, BetterPowerBarAltDB.x, BetterPowerBarAltDB.y)
	else
		--set the text to always show when first loaded (assuming you move the bar eventually)
		--SetCVar(cvar, 1, cvarLabel)
	end
	
	if BetterPowerBarAltDB.scale then
		PlayerPowerBarAlt:SetScale(BetterPowerBarAltDB.scale)
	end
end)

overlay:SetScript("OnEnter", function(self) UnitPowerBarAlt_OnEnter(self:GetParent()) end)
overlay:SetScript("OnLeave", function(self) UnitPowerBarAlt_OnLeave(self:GetParent()) end)

--in case the frame is under the ui or offscreen
_G["SLASH_BETTERPOWERBARALT1"] = "/bpba"
_G.SlashCmdList["BETTERPOWERBARALT"] = function(input)
	if input:trim() == "reset" then
		PlayerPowerBarAlt:ClearAllPoints()
		PlayerPowerBarAlt:SetPoint("CENTER")
	end
end
